package com.example;

import com.example.entity.Book;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SenderController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping
    public String sendBook(@RequestParam("name") String name, @RequestParam("author") String author) {

        this.rabbitTemplate.convertAndSend("codedef.delayed", "hello", new Book("10s", "ww"), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) {
                message.getMessageProperties().setDelay(1000 * 10 );
                return message;
            }
        });
        this.rabbitTemplate.convertAndSend("codedef.delayed", "hello",  new Book("5s", "ww"), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) {
                message.getMessageProperties().setDelay(1000 * 5 );
                return message;
            }
        });

        return "SEND SUCCESS";
    }

}
